import { ViteSSG, RouterOptions } from "vite-ssg";
import { setupLayouts } from 'virtual:generated-layouts'
import generatedRoutes from '~pages'
import Previewer from 'virtual:vue-component-preview'
import App from './App.vue'
import type { UserModule } from './types'
import "virtual:svg-icons-register";
import SocketIO from "@/server/Socket.IO";

import { useReload } from "@/hooks";
import { SERVER_ADDRESS } from '@/config';

import '@unocss/reset/tailwind.css'
import './styles/main.css'
import 'uno.css'


const routes = setupLayouts(generatedRoutes)


export const createApp = ViteSSG(
  App,
  {
    routes,
    base: import.meta.env.BASE_URL,
    scrollBehavior(to, from, savedPosition) {
      if (from.fullPath === '/bbs') {
        return {
          top: 0,
          left: 0,
        };
      } else if (to.meta!.savedPosition) {
        return {
          top: (to.meta!.savedPosition && window.scrollY) || 0,
          left: 0,
        };
      } else {
        return {
          top: 0,
          left: 0,
        };
      }
      // if (savedPosition) {
      //   return savedPosition;
      // }
    },
  } as RouterOptions,
  (ctx) => {
    // install all modules under `modules/`
    Object.values(
      import.meta.glob<{ install: UserModule }>("./modules/*.ts", {
        eager: true,
      })
    ).forEach((i) => i.install?.(ctx));
    ctx.app.use(Previewer).use(SocketIO, {
      connection: SERVER_ADDRESS,
      options: { withCredentials: true, transports: ["websocket"] },
    });
    ctx.app.provide("reload",useReload());
  }
);






